#include<bits/stdc++.h>

using pii = std::pair<int, int>;
using i64 = long long;
using u64 = unsigned long long;

#define for_range(i, l, r) for(int i = (l); i < (r); i++)
#define rfor_range(i, l, r) for(int i = (r)-1; i >= (l); i--)
#define for_each(i, l, r) for(int i = (l); i <= (r); i++)
#define rfor_each(i, l, r) for(int i = (r); i >= (l); i--)

#define chmin(a, b) a = std::min(a, (b))
#define chmax(a, b) a = std::max(a, (b))
#define all(a) a.begin(), a.end()

const int MAXA = 1e6;

void solve(){
    int N;
    std::cin >> N;
    std::vector<int> a(N);
    for_range(i, 0, N){
        std::cin >> a[i];
    }

    std::vector<int> lst(MAXA + 1, -1);
    std::vector<int> pair(N + 1, -1);
    for_range(i, 0, N){
        if(lst[a[i]] != -1){
            pair[i] = lst[a[i]];
        }
        lst[a[i]] = i;
    }

    std::vector<i64> pre(N);
    for_range(i, 0, N-1){
        pre[i+1] += pre[i];
        if(a[i] == a[i+1]){
            pre[i+1] += a[i];
        }
    }

    std::vector<i64> dp(N+1);
    for_each(i, 1, N){
        if(pair[i] != -1){
            chmax(dp[i-1], (pair[i]>0?dp[pair[i]]:0) + std::max(pre[i-1] - pre[pair[i]+1], 0ll) + a[i]);
        }
        chmax(dp[i], dp[i-1]);
   }

    std::cout << dp[N-1] << "\n";
}

int main(){
    assert(freopen("color.in", "r", stdin));
    assert(freopen("color.out", "w", stdout));

    std::cin.tie(0);
    std::ios::sync_with_stdio(0);

    int T = 1;
    std::cin >> T;
    for_range(t, 0, T){
        solve();
    }
    return 0;
}
// g++ color.cpp -O2 -Wall -Wextra -Wshadow -Wconversion -fsanitize=undefined -fsanitize=address -D__GLIBCXX_DEBUG -o color
// // /usr/bin/time